package com.lagou.rpc.consumer.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.Callable;

public class RpcClientHandler extends SimpleChannelInboundHandler<String>  implements Callable<String> {
    private  ChannelHandlerContext context;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    private  String msg;
    private String responseMsg;
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
         this.context = ctx;
    }
    @Override
    protected synchronized void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
        responseMsg = msg;
        notify();
    }

    @Override
    public synchronized String call() throws Exception {
        context.writeAndFlush(msg);
        wait();
        return responseMsg;
    }
}
